import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  UseGuards,
  SetMetadata,
} from '@nestjs/common';
import { GuardService } from './guard.service';
import { CreateGuardDto } from './dto/create-guard.dto';
import { UpdateGuardDto } from './dto/update-guard.dto';

import { ApiTags, ApiOperation, ApiParam } from '@nestjs/swagger';

// 守卫装饰器
import { RoleGuard } from './role/role.guard';

import { Role, ReqUrl } from './role/role.decorator';
@ApiTags('守卫的接口')
@Controller('guard')
@UseGuards(RoleGuard)
export class GuardController {
  constructor(private readonly guardService: GuardService) {}

  @Post()
  create(@Body() createGuardDto: CreateGuardDto) {
    return this.guardService.create(createGuardDto);
  }

  @Get()
  @Role('admin') // 自定义装饰器，用于角色守卫判断用户是否有权限访问该接口
  @ApiOperation({ summary: '摘要', description: '一个详细的描述' })

  // @SetMetadata('roles', ['admin', 'commen']) // 自定义元数据，用于角色守卫判断用户是否有权限访问该接口
  findAll(@ReqUrl('123') url: string) {
    console.log(url);
    return this.guardService.findAll();
  }

  @Get(':id')
  @ApiParam({
    name: 'id',
    description: '用户ID',
    required: true,
    type: 'string',
    example: '123',
  })
  findOne(@Param('id') id: string) {
    return this.guardService.findOne(+id);
  }

  @Patch(':id')
  update(@Param('id') id: string, @Body() updateGuardDto: UpdateGuardDto) {
    return this.guardService.update(+id, updateGuardDto);
  }

  @Delete(':id')
  remove(@Param('id') id: string) {
    return this.guardService.remove(+id);
  }
}
